/*
 * @Author: ducafecat
 * @Description: 列表页
 * @Date: 2018-04-13 16:13:27
 * @Last Modified by: hans.taozhiwei
 * @Last Modified time: 2018-04-13 16:15:33
 */
import { list, add, remove, update, changeState } from '@/api/hmmm/tags.js'
import { simple } from '@/api/hmmm/subjects.js'
const Tags = {
  namespaced: true,
  state: {
    name: '',
    list: [],
    total: 0,
    // 简易学科列表
    simpleList: []
  },
  mutations: {
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_LIST (state, newlist) {
      state.list = newlist
    },
    SET_TOTAL (state, total) {
      state.total = total
    },
    SET_SIMPLE_LIST (state, newlist) {
      state.simpleList = newlist
    }
  },
  actions: {
    setName ({ commit }, userInfo) {
      const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        commit('SET_NAME', username)
        resolve()
      })
    },
    // 请求服务器,获取学科列表
    async updatelist ({ commit }, params) {
      const { items, counts } = await list(params)
      commit('SET_LIST', items)
      commit('SET_TOTAL', counts)
    },
    // 请求服务器,新增学科
    async updateadd (context, data) {
      await add(data)
    },
    // 请求服务器,修改学科
    async updateaedit (context, data) {
      await update(data)
    },
    // 请求服务器,删除学科
    async updatedel (context, data) {
      await remove(data)
    },
    // 获取简易列表
    async updateSimplelist ({ commit }, params) {
      const { data } = await simple(params)
      commit('SET_SIMPLE_LIST', data)
    },
    async updateEditDirectorys (context, data) {
      await changeState(data)
    }
  }
}

export default Tags
